package kr.co.goodwilldd.service.menus.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import kr.co.goodwilldd.entity.menus.model.MenuEntity;
import kr.co.goodwilldd.entity.menus.model.MenuItemEntity;
import kr.co.goodwilldd.framework.flexjson.NullExcludeTransformer;
import kr.co.goodwilldd.framework.repository.DefaultRepository;
import kr.co.goodwilldd.repository.menus.IMenuRepository;
import kr.co.goodwilldd.service.menus.IMenuService;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Service
public class MenuServiceImpl implements IMenuService {

	private static final Logger logger = LoggerFactory
			.getLogger(MenuServiceImpl.class);

	@Autowired
	DefaultRepository defaultRepository;

	@Autowired
	IMenuRepository iMenuRepository;

	@Override
	@PostConstruct
	public void init() {
		defaultRepository.doCreateTableMenus();
		defaultRepository.doCreateSequenceMenu();
	}

	@Override
	public MenuEntity getTree() {
		return MakeMenuUtil.make(iMenuRepository.getTree());
	}

	@Override
	public String getTree2() {
		String result = (new JSONSerializer().transform(
				new NullExcludeTransformer(), void.class).exclude("*.class"))
				.deepSerialize(getTree());
		if (logger.isDebugEnabled())
			logger.debug("pasing tree result : {}", result);
		return result;
	}

	@Override
	public String getMenuSequence() {
		return iMenuRepository.getMenuSequence();
	}

	enum USERROLE {
		ANONYMOUS {
			@Override
			public int getNum() {
				return 1;
			}
		}, ROLE_USER {
			@Override
			public int getNum() {
				return 2;
			}
		}, ROLE_MANAGER {
			@Override
			public int getNum() {
				return 3;
			}
		}, ROLE_SUPERVISOR {
			@Override
			public int getNum() {
				return 3;
			}
		};
		public abstract int getNum();
	}

	@Override
	public void doUpdateMenu(String data) throws Exception {
		logger.debug("input data : {}", data);
		logger.debug("input data : {}", HtmlUtils.htmlUnescape(data));
		try {
			JSONDeserializer<List<Map<String, Object>>> deserializer = new JSONDeserializer<List<Map<String, Object>>>();
			List<Map<String, Object>> parsing = deserializer
					.deserialize(HtmlUtils.htmlUnescape(data));
			MenuItemEntity menuItem = null;
			List<Integer> child = new ArrayList<Integer>();
			for (Map<String, Object> map : parsing) {
				menuItem = new MenuItemEntity();
				BeanUtils.populate(menuItem, map);
				logger.debug("tree datas : {}", menuItem.toString());
				if (!menuItem.getTypes().equals("ROOT"))
					iMenuRepository.doUpdateMenuItem(menuItem);

				child.add(menuItem.getChildSeq());
			}
			iMenuRepository.doDeleteMenu(Collections.singletonMap("childs",
					child));
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public MenuItemEntity getMenu(Integer menuSeq) {
		return iMenuRepository.getMenu(menuSeq);
	}
}
